1   /*
2    * Copyright (C) 2011 The Guava Authors
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.google.common.base;
18  
19  import static com.google.common.base.Preconditions.checkNotNull;
20  
21  import com.google.common.annotations.Beta;
22  import com.google.common.annotations.GwtCompatible;
23  
24  import java.io.Serializable;
25  
26  import javax.annotation.Nullable;
27  
28  /**
29   * Utility methods for working with {@link Enum} instances.
30   *
31   * @author Steve McKay
32   *
33   * @since 9.0
34   */
35  @GwtCompatible(emulated = true)
36  @Beta
37  public final class Enums {
38  
39    private Enums() {}
40  
41    /**
42     * Returns an optional enum constant for the given type, using {@link Enum#valueOf}. If the
43     * constant does not exist, {@link Optional#absent} is returned. A common use case is for parsing
44     * user input or falling back to a default enum constant. For example,
45     * {@code Enums.getIfPresent(Country.class, countryInput).or(Country.DEFAULT);}
46     *
47     * @since 12.0
48     */
49    public static <T extends Enum<T>> Optional<T> getIfPresent(
50        Class<T> enumClass, String value) {
51      checkNotNull(enumClass);
52      checkNotNull(value);
53      return Platform.getEnumIfPresent(enumClass, value);
54    }
55  
56    /**
57     * Returns a converter that converts between strings and {@code enum} values of type
58     * {@code enumClass} using {@link Enum#valueOf(Class, String)} and {@link Enum#name()}. The
59     * converter will throw an {@code IllegalArgumentException} if the argument is not the name of
60     * any enum constant in the specified enum.
61     *
62     * @since 16.0
63     */
64    public static <T extends Enum<T>> Converter<String, T> stringConverter(final Class<T> enumClass) {
65      return new StringConverter<T>(enumClass);
66    }
67  
68    private static final class StringConverter<T extends Enum<T>>
69        extends Converter<String, T> implements Serializable {
70  
71      private final Class<T> enumClass;
72  
73      StringConverter(Class<T> enumClass) {
74        this.enumClass = checkNotNull(enumClass);
75      }
76  
77      @Override
78      protected T doForward(String value) {
79        return Enum.valueOf(enumClass, value);
80      }
81  
82      @Override
83      protected String doBackward(T enumValue) {
84        return enumValue.name();
85      }
86  
87      @Override
88      public boolean equals(@Nullable Object object) {
89        if (object instanceof StringConverter) {
90          StringConverter<?> that = (StringConverter<?>) object;
91          return this.enumClass.equals(that.enumClass);
92        }
93        return false;
94      }
95  
96      @Override
97      public int hashCode() {
98        return enumClass.hashCode();
99      }
100 
101     @Override
102     public String toString() {
103       return "Enums.stringConverter(" + enumClass.getName() + ".class)";
104     }
105 
106     private static final long serialVersionUID = 0L;
107   }
108 }
109